set
A set answers one question: is this value in here? It does not store where the value is, what goes with it, or when it was added. A value is either a member of the set or it is not, and a set never holds the same value twice. Picture a guest list at a door: you check a name, and the answer is yes or no.
That narrow job is the point. A set answers "have I seen this before" very quickly, and the time does not grow as the set fills up.
Reach for a set when you are tracking which things have happened:
/// @type {set<int>}
/// @capacity 64
var visited = [];/// @type says what kind of value the set holds. Here every member is an int. /// @capacity is the most members the set can hold, and leaving it off gives you 8. Like every container on the GBA, its space is set aside when the game is built, not grown while it runs.
The same set with no comment: var visited = set_create(64, 0); (an empty set of whole numbers that holds 64) or var visited: set<int, 64> = [];. See set_create. All three are the same set, and a set given no size by any of them holds 8.
A starting list in brackets adds each value as a member, and a repeated value counts once: [1, 2, 2, 3] starts as a set of three members, 1, 2 and 3.
| Function | What it does | It gives back |
|---|---|---|
set_add(set, value) | makes value a member | true if value is a member afterwards, false if it was new and the set was full |
set_has(set, value) | asks whether value is a member | true or false |
set_contains(set, value) | exactly the same as set_has, under the name the other containers use | true or false |
set_remove(set, value) | takes value out | true if it was a member, false if not |
set_length(set) | counts the members | the count |
set_clear(set) | empties it | nothing |
set_copy(set) | makes a second set just like it, with the same room | the new set |
set_add, set_has and set_remove each take about the same short time whether the set holds five members or five hundred. Asking the same question of a list with list_contains checks the items one after another, so it slows down as the list grows.
set_add on a value that is already a member does nothing, and it still returns true. The answer means "is this value in the set now," not "did this call change anything." So false means one thing only: the value was new and there was no room.
That makes a duplicate harmless. You never need to ask set_has before you set_add, and set_length counts each value once.
/// @type {set<int>}
/// @capacity 64
var visited = [];
var tile = 3 * 8 + 2; // a tile's number, from its row and column
var first = set_add(visited, tile); // true - now a member
var again = set_add(visited, tile); // true - already a member, nothing changed
var count = set_length(visited); // 1
var seen = set_has(visited, tile); // true
set_remove(visited, tile);
var still = set_has(visited, tile); // falseA set's capacity must be a power of two: 2, 4, 8, 16, 32, and so on. The Game Boy Advance module's container a set is built on stores its members in a hash table and refuses any other size. The default of 8 already follows the rule. Write /// @capacity 12 and the project is refused before the game runs, with a message naming the next size up (16). The queue and the map have the same rule.
Each member is turned into a lookup number, called hashing, so it has to be a kind of value that can be. These work: int, real, bool, string, color, an instance reference (ref<obj_enemy>), and any of your project's asset ids. A struct cannot be a member, and the project is refused where the set is declared. To track "have I seen this struct," use a list and list_contains, which is slower but accepts any type.
Instance references do work, so a set can track instances. Two references are the same member when they point at the same instance. A reference stays a valid member even after its instance is destroyed: the set only compares the references and never looks at the instance behind one.
Here a set remembers which enemy has been dealt with, so each is handled once:
/// @type {set<ref<obj_doc_enemy>>}
var already_hit = [];
var target = instance_nearest(x, y, obj_doc_enemy);
if (not set_has(already_hit, target)) {
set_add(already_hit, target);
// hurt the enemy here - the next time it comes up, set_has says yes and it is skipped
}In a real game the set would be an instance variable, declared once in the Create event, so it keeps its members from one frame to the next. A variable declared with var inside an event disappears when that event ends.
A set can say whether a value is a member. It cannot list its members or hand them out in order. There is no loop over a set, and no "give me any member." The members are not stored in the order you added them.
When a game needs to go through every member, keep a list beside the set. Add to both together, and walk the list with [i]. The set is then only for the fast yes-or-no question. The Data containers page describes this pattern.
[i]A set cannot be indexed: visited[0] is refused when the project is checked, because members have no positions to index. Accessors covers this across all the containers.
Reach for a set when you only need to know whether something has happened, and there is nothing more to store about it. If each thing needs a value stored with it, such as a score, use a map. If the order matters, or you need to go through every item, use a list. Data containers compares all of them side by side.